package top.wettis.chat.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.wettis.chat.constant.ResultType;
import top.wettis.chat.mapper.ObjectivesMapper;
import top.wettis.chat.mapper.TaskMapper;
import top.wettis.chat.mapper.TodayPunchMapper;
import top.wettis.chat.mapper.UserMapper;
import top.wettis.chat.pojo.DO.Objectives;
import top.wettis.chat.pojo.DO.TodayPunch;
import top.wettis.chat.pojo.VO.Result;
import top.wettis.chat.service.ITodayPunchService;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;



/**
 * <p>
 * 每日打卡 服务实现类
 * </p>
 *
 * @author wettis
 * @since 2023-03-25
 */
@Service
public class TodayPunchServiceImpl extends ServiceImpl<TodayPunchMapper, TodayPunch> implements ITodayPunchService {
    @Autowired
    private  TodayPunchMapper todayPunchMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ObjectivesMapper objectivesMapper;

    @Autowired
    private TaskMapper taskMapper;

    @Override
    public Result<Object> insert(Integer id) {
        int cur_level;

        //获取今日打卡日期
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dateTimeString = now.format(formatter);

        //今日是否打卡
        TodayPunch today = query().eq("login_time", dateTimeString).eq("user_id" , id).one();
        if(today != null){
            return Result.failInfo(ResultType.SAVE , "今日已打卡");
        }

        //
        TodayPunch todayPunch = new TodayPunch();
        todayPunch.setUserId(id);
        todayPunch.setLoginTime(dateTimeString);
        todayPunch.setMonth(DateTime.now().month() + 1);
        save(todayPunch);


        String before = todayPunchMapper.getBefore(dateTimeString, id);

        if (before==null){
            Objectives objectives = objectivesMapper.selectcur(id);
            cur_level = objectives.getLevel();
            userMapper.updateLevel(cur_level , id);
            objectivesMapper.punch(objectives.getTaskId());
            taskMapper.punch(id , cur_level);
            userMapper.updatePoint(id , 1.0);
            return Result.successInfo(ResultType.SAVE , "今日打卡成功");
        }
        DateTime t1 = DateTime.of(before, "yyyy-MM-dd");
        DateTime t2 = DateTime.of(dateTimeString, "yyyy-MM-dd");
        long dis = DateUtil.between(t1, t2, DateUnit.DAY);
        int m1 = t1.month();
        int m2 = t2.month();
        System.out.println(m1);
        System.out.println(m2);
        if(m2 == m1){
            if(dis == 1){

                userMapper.updateConsecutive(id);
            }else {
                userMapper.ResetConsecutive(id);
            }
        }
        else {
            todayPunchMapper.deleteLastMonth(m1 + 1 , id);
            userMapper.ResetConsecutive(id);
        }
//        userMapper.ResetConsecutive(id);
        Objectives objectives = objectivesMapper.selectcur(id);
        cur_level = objectives.getLevel();
        userMapper.updateLevel(cur_level , id);
        objectivesMapper.punch(objectives.getOrders());
        taskMapper.punch(id , cur_level);
        double tt = taskMapper.ttdaka(id) * 1.0;
        double c = userMapper.getCon(id) * 1.0;
        double p = (tt-c) + c * 1.5;
        System.out.println(p);
        userMapper.updatePoint(id , p);
        return Result.successInfo(ResultType.SAVE , "今日打卡成功");
    }

    @Override
    public Result<Object> countPunch(int uid) {
        int cout = todayPunchMapper.Count(DateTime.now().month() + 1, uid);
        System.out.println(DateTime.now().month() + 1);
        return Result.successInfo(ResultType.GET , cout);
    }

    @Override
    public Result<Object> getall(int uid) {
        List<String> dates = todayPunchMapper.selectDate(DateTime.now().month() , uid);
        return Result.successInfo(ResultType.GET , dates);
    }
}
